package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @auther XuGeGe
 * @date 2022/6/28
 * 按年计算
 */
public class Year {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf1.parse("2022-02-28");
        System.out.println("开始时间：" + sdf1.format(startTime));
        int account = 3;
        System.out.println("总次数： " + account);
        int month = 1;
        int day = 29;

        //下一次随访时间
        Date nextFollowDate = new Date();
        //结束时间
        Date endDate = new Date();
        //计算下一次随访计划
        if (judge(getYear(startTime), month, day)) {
            nextFollowDate = getDate(getYear(startTime), month, day);
        } else {
            nextFollowDate = getDate(getYear(startTime), month, getDayByYearAndMonth(getYear(nextFollowDate), month));
        }

        //d1<d2
        if (nextFollowDate.compareTo(startTime) < 0) {
            nextFollowDate = addYear(nextFollowDate, 1);
        }
        System.out.println("下一次随访计划： " + sdf1.format(nextFollowDate));

        //计算结束日期
        endDate = addYear(nextFollowDate, account - 1);
        if (getMaxMonthNumber(endDate) >= day) {
            endDate = getDateByNumber(endDate, day);
        }
        endDate = addDay(endDate, 1);
        System.out.println("结束时间： " + sdf1.format(endDate));

    }

    /**
     * 指定年月日
     */
    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 根据当前日期设置当前月份号数
     */
    public static Date getDateByNumber(Date date, int number) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, number);
        return cal.getTime();
    }

    /**
     * 根据当前日期设置当前月份
     */
    public static Date getDateByMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, month - 1);
        return cal.getTime();
    }

    /**
     * 获取号数
     */
    public static int getNumber(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_MONTH);
        return i;
    }

    /**
     * 获取当前日期月份最大天数
     */
    public static int getMaxMonthNumber(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //获取当前日期月份最大天数
        int lastNumber = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return lastNumber;
    }

    //当前日期 + n 天
    public static Date addDay(Date startTime, int dayNumber) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.DATE, dayNumber); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    //增加月份
    public static Date addMonth(Date startTime, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.MONTH, month); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    //获取日期月份
    public static int getMonth(Date startTime) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        int i = calendar.get(Calendar.MONTH) + 1;
        return i; //这个时间就是日期往后推一天的结果
    }

    //增加年份 + n
    public static Date addYear(Date startTime, int year) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.YEAR, year); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    //获取年份
    public static int getYear(Date startTime) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        int year = calendar.get(calendar.YEAR);
        return year;
    }

    //判断是否合法
    public static boolean judge(int y, int m, int d) {
        boolean p = false;

        if (m < 1 || m > 12) {
            p = false;
        } else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
            if (d <= 31) {
                p = true;
            } else {
                p = false;
            }
        } else if (m == 2) {
            if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) {
                if (d <= 29) {
                    p = true;
                } else {
                    p = false;
                }
            } else {
                if (d <= 28) {
                    p = true;
                } else {
                    p = false;
                }
            }
        } else {
            if (d <= 30) {
                p = true;
            } else {
                p = false;
            }
        }
        return p;
    }

    /**
     * 根据年月获取 天数
     */
    public static int getDayByYearAndMonth(int year, int month) {
        int days = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    days = 29;
                else
                    days = 28;
                break;
            default:
                System.exit(0);
        }
        return days;
    }

}
